package myjiava6.com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class Hn
{
	public static void main(String[] args)
	{
		System.out.println("====\\===");
		String s ="G:\\20180626-io\\笔记\\areacode.txt";
		/*读取文件*/
		//file(s);
		String s1 = "41";
		/*查询河南所在的城市*/
		hn(s,s1);
	}

	private static void hn(String s,String s1)
	{
		/*查询河南地级市
		 * 并存储到另一个文件中
		 * 需要一边读一边写*/
		BufferedReader  br  = null;
		/*写数据*/
		BufferedWriter  bw = null;
		/*建立一个文件*/
		File ln = new File(s);
		/*获取父目录*/
		File p = ln.getParentFile();
		System.out.println(p.getAbsolutePath()/*绝对路径*/);
		String name1=ln.getName(/*抽取文件名*/);
		/*取到.之后的直接覆盖追加到结尾
		 * . 需要转义 用\\*/
		String name =name1.replaceAll("\\.", "_cca.");
		/*拼装目录*/
		File fi = new File(p,name);
		System.out.println(fi.getAbsolutePath());
		try
		{
			br =new BufferedReader(new InputStreamReader(new FileInputStream(ln), "GBK"));
			bw =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fi),"UTF-8"));
			/*读取内容*/
			String si = "";
			int con =0 ; 
			while((si=br.readLine()) !=null)
			{
				/*用拆分方法如下*/
				String[] ie=si.split("\t");
				/*拆分之后的数组就两个字符串*/
				if(ie.length !=2)
				{
					/*跳过当前循环
					 * 继续执行下一个循环*/
					continue;
				}
				/*使用数组内容的时候防止下标越界*/
				String l1 =ie[1];
				String l2 = ie[0];
				if(l1.startsWith(s1) && l1.endsWith("00") && !l1.endsWith("0000"));
				{
					String w = con +"=="+l1+"=="+l2;
					System.out.println(w);
					/*写到文件中去*/
					bw.write(w);
					bw.newLine();
					con++;
				}
			}
			/*只要写了就要控水管*/
			bw.flush();
		} catch (UnsupportedEncodingException | FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try
			{
				if(br !=null)
				{
					br.close();
					br=null;
				}
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try
			{
				if(bw !=null)
				{
					bw.close();
					bw=null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	private static void file(String s)
	{
		BufferedReader br=null;
		try
		{
			br=new BufferedReader(new InputStreamReader((new FileInputStream(new File(s))), "GBK"));
			String le="";
			int cou = 0;
			while((le=br.readLine()) !=null)
			{
				System.out.println(cou+"=="+le);
				cou++;
			}
		} catch (UnsupportedEncodingException | FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try
			{
				if(br !=null)
				{
					br.close();
					br=null;
				}
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
